//
//  WBOriginalView.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 29/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit
import SDWebImage

class WBOriginalView: UIView {
    
    var statusViewModel: WBStatusViewModel? {
        didSet{
            
            userNameLabel.text  = statusViewModel?.screenName
            
            iconImageView.image = #imageLiteral(resourceName: "avatar_default_big")
            if
                let iconURLStr  = statusViewModel?.userIconURLStr,
                let iconURL     = URL(string: iconURLStr)
                {
                SDWebImageManager.shared().downloadImage(with: iconURL, options: [], progress: nil) { (downloadedImage, _, _, _, _) in
                    guard let downloadedImage = downloadedImage else {
                        return
                    }
                    downloadedImage.wb_asyncDrawImage(backgroundColor:UIColor.clear) { (roundImage) in
                        self.iconImageView.image = roundImage
                    }
                }
            }
            
            levelIcon.image = statusViewModel?.levelImage
            vipIcon.image = statusViewModel?.vipImage
            timeLabel.text = statusViewModel?.timeString
            sourceLabel.text = statusViewModel?.sourceString
            textLabel.text = statusViewModel?.text
            
            pictureView.statusViewModel = statusViewModel
        }
    }
    
    /// 用户头像
    lazy var iconImageView: UIImageView = UIImageView()
    /// 用户名label
    lazy var userNameLabel: UILabel = UILabel(title: "", fontSize: 15)
    /// 用户等级图标
    lazy var levelIcon: UIImageView = UIImageView()
    /// vip图标
    lazy var vipIcon: UIImageView  = UIImageView()
    /// 发布时间label
    lazy var timeLabel: UILabel = UILabel(title: "", fontSize: 10, textColor: UIColor.lightGray)
    /// 来源label
    lazy var sourceLabel: UILabel = UILabel(title: "", fontSize: 10, textColor: UIColor.lightGray)
    /// 原创微博文字的lable
    lazy var textLabel: UILabel = UILabel(title: "", fontSize: 15)
    
    lazy var pictureView: WBPictureView = WBPictureView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension WBOriginalView {
    
    func setupUI() {
        backgroundColor = UIColor.white
        
        addSubview(iconImageView)
        addSubview(userNameLabel)
        addSubview(levelIcon)
        addSubview(vipIcon)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        addSubview(pictureView)
        
        let padding     = cellRuler * 0.3
        let smallIconHW = cellRuler * 0.4
        
        iconImageView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(padding)
            make.size.equalTo(CGSize(width: cellRuler, height: cellRuler))
        }
        
        userNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView)
            make.left.equalTo(iconImageView.snp.right).offset(padding)
        }
        
        levelIcon.snp.makeConstraints { (make) in
            make.centerY.equalTo(userNameLabel)
            make.left.equalTo(userNameLabel.snp.right).offset(padding * 0.5)
            make.size.equalTo(CGSize(width: smallIconHW, height: smallIconHW))
        }
        
        vipIcon.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconImageView)
            make.centerX.equalTo(iconImageView.snp.right)
            make.size.equalTo(CGSize(width: smallIconHW, height: smallIconHW))
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(padding)
            make.bottom.equalTo(iconImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(padding)
            make.bottom.equalTo(iconImageView)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp.bottom).offset(padding)
            make.left.equalTo(iconImageView)
            make.right.equalToSuperview().offset(-padding)
        }
        
        //配图视图
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(textLabel.snp.bottom).offset(padding)
            make.left.equalTo(textLabel)
            make.size.equalTo(CGSize(width: screenWidth-2*padding, height: screenWidth-2*padding))
            make.bottom.equalToSuperview().offset(-padding)
        }
        
    }
    
}
